/**
 * 
 */
package table;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author sq
 *
 */
public class TableTools {
	//------All--------
	private static ArrayList<DataTable> tableList;

	static public ArrayList<DataTable> getAllTables(String path){
		
		tableList = new ArrayList<DataTable>();
		ApplicationContext context = new ClassPathXmlApplicationContext(path);
		Map<?, ?> tablesmap = context.getBeansOfType(DataTable.class);
		/*
		String[] BeanNames = context.getBeanDefinitionNames();
		for(int i=0; i < BeanNames.length; i++){
			DataTable tableBean = (DataTable)context.getBean(BeanNames[i], DataTable.class);
			tableList.add(tableBean);
		}
		 * 
		 */
		for(Object bean : tablesmap.values()) {
			DataTable table = (DataTable)bean;
			if(table != null){
				tableList.add(table);
			}
		}
		return tableList;
	}
	
	static public DataTable getBean (String classId, String path){
		DataTable datatablev = null;
		ApplicationContext context = new ClassPathXmlApplicationContext(path);
		if(null != context){
			datatablev = (DataTable) context.getBean(classId);
		}
		return datatablev;
	}

	/**
	 * Get List method paramenters from request 
	 * @param request
	 * @return Map Object which map List Method Paraments 
	 */
	static public Map<Object, Object> getListParaFromRequest(HttpServletRequest request){
		Map<Object, Object> dataMapList = null;

		int s = getStartPageFromRequest(request);
		int n = getPageSizeFromRequest(request);
		List<String[]> queryList = getQueryListFromRequest(request);
		List<String[]> sortList = getSortListFromRequest(request);
		
		return dataMapList;
	} 
	/**
	 * Get Start Page from request
	 * @param request
	 * @return Start Page Parament as integer
	 */
	static public int getStartPageFromRequest(HttpServletRequest request){
		int s = 0;
		if(null != request.getParameter("start")){
			String sPageStart = request.getParameter("start");
			if(sPageStart != null && sPageStart.toLowerCase().matches("([1-9][0-9]*)|([0-9])"))
			s = Integer.parseInt(sPageStart);
		}
		return s;
	}
	
	/**
	 * Get Page Size from request
	 * @param request
	 * @return Page Size Parament as integer
	 */
	static public  int getPageSizeFromRequest(HttpServletRequest request){
		int n = 500;
		if(null != request.getParameter("pagesize")){
			String sPageStart = request.getParameter("pagesize");
			if(sPageStart != null && sPageStart.toLowerCase().matches("([1-9][0-9]*)"))
			n = Integer.parseInt(sPageStart);
		}
		return n;
	}

	/**
	 * get query paramenters from request paramenters 
	 * @param request
	 * @return Query Parament Names and Values as List of Strings
	 */
	static public  List<String[]> getQueryListFromRequest(HttpServletRequest request){
		List<String[]> s = new ArrayList<String[]>();
		String[] r = new String[2];
		String colName = "";
		String colValue = "";
		try {
			Enumeration<String> paraNames = request.getParameterNames();
			while(paraNames.hasMoreElements()){
				colName = paraNames.nextElement();
				if(colName.startsWith("q-")){
					colValue = request.getParameter(colName);
					if(null != colValue && colValue.length()>0){
						colValue = new String(colValue.getBytes("ISO8859-1"), "UTF-8");
						r[0] = colName.substring(2); 
						r[1]= colValue;
						s.add(r);
					}
				}
			}
		} catch (Exception e) {
				e.printStackTrace();
		}
		return s;
	}

	/**
	 * get sort paramenters from request paramenters 
	 * @param request
	 * @return Sort Parament Names and Values as List of Strings
	 */
	static public  List<String[]> getSortListFromRequest(HttpServletRequest request){
		List<String[]> s = new ArrayList<String[]>();
		String colName = "";
		String colValue = "";
		String[] r = new String[2];
		
		Enumeration<String> paraNames = request.getParameterNames();
		while(paraNames.hasMoreElements()){
			colName = paraNames.nextElement();
			if(colName.startsWith("s-")){
				colValue = request.getParameter(colName);
				if(null != colValue && colValue.length()>0){
					r[0] = colName.substring(2); 
					if(colValue.equalsIgnoreCase("DESC")){
						r[1]= "DESC";
					}else{
						r[1]= "ASC";
					}
					s.add(r);
				}
			}
		}
		return s;
	}
	
}
